package com.dxq.test.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.db.FeedGroupFood;
import com.dxq.test.db.Food;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.dxq.test.utils.DialogClickListener;
import com.dxq.test.utils.MyUtil;
import com.j256.ormlite.dao.ForeignCollection;

import java.util.Date;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/11/18
 * Describe: 饲料添加、修改、删除页面
 */
public class FoodAddActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack;
    private TextView tvTitle, tvSet;
    private LinearLayout llCode;
    private TextView tvCode;
    private EditText etName, etInfo;
    private Button btnSubmit;

    private Food selectFood;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_food_add);
        selectFood = (Food) getIntent().getSerializableExtra("food");
        tablesDao = new TablesDao(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tvTitle);
        tvSet = findViewById(R.id.tvSet);
        llCode = this.findViewById(R.id.llCode);
        tvCode = findViewById(R.id.tvCode);
        etName = findViewById(R.id.etName);
        etInfo = findViewById(R.id.etInfo);
        btnSubmit = findViewById(R.id.btnSubmit);
        if (null == selectFood) {
            tvTitle.setText("添加饲料");
            tvSet.setVisibility(View.GONE);
            llCode.setVisibility(View.GONE);
        } else {
            tvTitle.setText("修改饲料");
            tvSet.setVisibility(View.VISIBLE);
            tvSet.setText("删除");
            llCode.setVisibility(View.VISIBLE);
            tvCode.setText(selectFood.getCode());
            etName.setText(selectFood.getName());
            etInfo.setText(selectFood.getInfo());
        }

        ivBack.setOnClickListener(this);
        tvSet.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                this.finish();
                break;

            case R.id.tvSet://删除
                MyUtil.normalDialog(FoodAddActivity.this, "操作提示", "是否删除饲料信息？",
                        "取消", "确定", new DialogClickListener() {

                            @Override
                            public void onNegativeClick() {

                            }

                            @Override
                            public void onPositiveClick() {
                                deleteFood();
                            }
                        });
                break;

            case R.id.btnSubmit://提交
                submit();
                break;
        }
    }

    //删除food
    private void deleteFood() {
        //判断该饲料是否已与饲喂关联
        List<FeedGroupFood> feedGroupFoods = tablesDao.queryFeedGroupFoodByFood(selectFood);
        if (!ObjectUtils.isEmpty(feedGroupFoods)) {
            //存在关联数据，不允许删除
            String info = "'" + selectFood.getName() + "'关联了饲喂信息，不允许删除";
            ToastUtils.showShort(info);
            MyUtil.normalDialogOne(FoodAddActivity.this,"操作提示", info);
            return;
        }
        //如果不关联则允许删除
        int result = tablesDao.deleteFood(selectFood);
        if (result != 1) {
            ToastUtils.showShort("删除失败, food");
            return;
        }
        ToastUtils.showShort("删除成功");
        this.finish();
    }

    private void submit() {
        //查询当前所选择牧场
        Pasture pasture = tablesDao.queryPastureById(App.select_pasture_id);
        String name = etName.getText().toString();
        String info = etInfo.getText().toString();
        if (name.equals("")) {
            ToastUtils.showShort("输入饲料名称");
            return;
        }
        if (info.equals("")) {
            ToastUtils.showShort("输入饲料配比");
            return;
        }
        if (null == selectFood) {
            //新增
            //1、判断饲料名称是否已经添加
            ForeignCollection<Food> foodList = pasture.getFoods();
            if (!ObjectUtils.isEmpty(foodList)) {
                for (Food food1 : foodList) {
                    if (food1.getName().equals(name)) {
                        ToastUtils.showShort("当前饲料名称已添加");
                        return;
                    }
                }
            }
            String code = "SL" + MyUtil.date2String(new Date());
            Food f = new Food(code, name, info, TimeUtils.getNowString(), pasture);
            //2、创建饲料
            int result = tablesDao.insertFood(f);
            if (result == 1) {
                ToastUtils.showShort("新增成功");
                this.finish();
                return;
            }
            ToastUtils.showShort("新增失败，food");
        } else {
            //修改
            Food f = tablesDao.queryFoodById(selectFood.getFood_id());
            f.setName(name);
            f.setInfo(info);
            //f.setTime(TimeUtils.getNowString());
            int result = tablesDao.updateFood(f);
            if (result == 1) {
                ToastUtils.showShort("更新成功");
                this.finish();
                return;
            }
            ToastUtils.showShort("更新失败，food");
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
